package frontend_package;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import backend_package.Packet;
import database_package.SqliteDatabase;

/**
 * The masthead displays the status of the connection and what user is is
 * currently connected. The user may also logout from this panel.
 * 
 * @author Kyler
 * 
 */
public class HeaderPanel extends Observable implements Observer {
	/** panel for header */
	private JPanel pnlHeader;
	/** label for account */
	private JLabel lblAccount;
	/** button status */
	private JButton btnStatus;
	/** log out button */
	private JButton btnLogout;
	/** Help button */
	private JButton btnHelp;

	/** boolean for online/offline statuses */
	private boolean isOnline = false;
	private Image image2;
	private Image image3;
	/**
	 * @author Kyler
	 */
	public HeaderPanel() {
		this("");

	}

	/**
	 * @author Kyler
	 * @author Aidana - edited
	 * @param theAccount
	 */
	public HeaderPanel(String theAccount) {
		pnlHeader = new JPanel(new BorderLayout());
		JPanel contentPanel = new JPanel(new GridBagLayout());
		contentPanel.setBackground(new Color(255, 255, 255));
		pnlHeader.add(contentPanel, BorderLayout.WEST);
		pnlHeader.setBackground(new Color(255, 255, 255));

		setAccount(theAccount); // Creates label
		setStatus(isOnline); // Creates button

		btnLogout = new JButton("Logout");
		btnLogout.setBackground(new Color(255, 255, 255));

		btnHelp = new JButton("Help");
		btnHelp.setBackground(new Color(255, 255, 255));

		addListeners();
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.WEST;

		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.insets = new Insets(1, 1, 1, 1);

		contentPanel.add(btnStatus, gbc);

		gbc.gridy++;
		contentPanel.add(btnLogout, gbc);

		gbc.gridx = 1;
		gbc.gridy = 0;
		contentPanel.add(lblAccount, gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 1;
		contentPanel.add(btnHelp, gbc);

	}
	/**
	 * @author Kyler
	 */
	private void addListeners(){
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setAccount("");
				setChanged();
				notifyObservers(new Packet("hp_logoutuserpanel"));
			}
		});
		btnHelp.addActionListener(new ActionListener() {
			String s = "Users and passwords:" +
					"\n john pass" +
					"\n jerry pass" +
					"\n mary pass" +
					"\n sam pass" +
					"\n admin adminpass";
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(getPanel(), s);
			}
		});
	}
	/**
	 * @author Kyler
	 * @return
	 */
	public JPanel getPanel() {
		return pnlHeader;
	}

	/**
	 * @author Kyler
	 */
	public void toggleStatus() {
		isOnline = !isOnline;
		setStatus(isOnline);
	}

	/**
	 * @author Kyler
	 * @author Aidana - edited
	 * @param status
	 */
	private void setStatus(boolean status) {

		if (btnStatus == null) {
			btnStatus = new JButton();

			btnStatus.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					toggleStatus(); // Toggle button
				}
			});
		}

		if (status) // Currently online
		{

			URL url = getClass().getResource("/status_green_circle.png");
			Image image = Toolkit.getDefaultToolkit().getImage(url);

			image2 = image.getScaledInstance(16, 16,
					java.awt.Image.SCALE_SMOOTH);
	
			btnStatus.setIcon(new ImageIcon(image2));
			btnStatus.setBackground(new Color(255, 255, 255));
			btnStatus.setText("Online");
		} else // Offline
		{	

			URL url = getClass().getResource("/status_red_circle.png");
			Image image = Toolkit.getDefaultToolkit().getImage(url);

			image3 = image.getScaledInstance(16, 16,
					java.awt.Image.SCALE_SMOOTH);
			
			btnStatus.setIcon(new ImageIcon(image3));
			btnStatus.setBackground(new Color(255, 255, 255));
			btnStatus.setText("Offline");
		}
	}

	/**
	 * @author Kyler
	 * @param account
	 */
	private void setAccount(String account) {
		if (lblAccount == null) {
			lblAccount = new JLabel("Account: " + account);
		} else {
			lblAccount.setText("Account: " + account);
		}
	}

	/**
	 * If the database sends userdata, the account is updated.
	 * 
	 * @author Kyler
	 */
	@Override
	public void update(Observable o, Object arg) {
		Packet packet = (Packet) arg;

		if (o instanceof SqliteDatabase
				&& packet.getTag().equals("db_userdata_hp")) {
			setAccount(packet.getUsername());
		}

	}
}
